package cn.jy.map.service.impl;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.jy.map.domain.GjzHct;
import cn.jy.map.domain.GjzHctExample;
import cn.jy.map.domain.UserSet;
import cn.jy.map.domain.XyDmRounds;
import cn.jy.map.jedis.JedisClient;
import cn.jy.map.mapper.GjzHctMapper;
import cn.jy.map.mapper.HCTAnalysisMapper;
import cn.jy.map.mapper.UpdateHctMapper;
import cn.jy.map.service.HCTAnalysisService;
import cn.jy.map.utils.DateUtils;
import cn.jy.map.utils.GjzHctResult;
import cn.jy.map.utils.NurseResult;
import cn.jy.map.utils.ParameterResult;
import cn.jy.map.utils.PlasmaMessage;
import cn.jy.map.utils.Statistical;
import cn.jy.map.utils.StringUtil;
import cn.jy.nvr.datasource.DataSourceConst;
import cn.jy.nvr.datasource.DataSourceContextHolder;

/**
 * @author XiongJing
 * @version 1.0
 * @date 2018年7月17日 下午1:45:07 类说明:hct值统计
 */
@Service("hCTAnalysisService")
@Transactional(readOnly = true)
public class HCTAnalysisServiceImpl implements HCTAnalysisService {

	@Autowired
	private HCTAnalysisMapper hCTAnalysisMapper;

	@Autowired
	private JedisClient jedisClient;

	@Autowired
	private GjzHctMapper gjzHctMapper;

	@Autowired
	private UpdateHctMapper updateHctMapper;

	/**
	 * 得到所有的护士
	 */
	@Override
	public List<UserSet> getNurseAll() {

		return hCTAnalysisMapper.getNurseAll();
	}

	/**
	 * 得到符合条件的所有浆员ID(l7年-今)
	 * 
	 * @return
	 */
	public List<Integer> getPlasma() {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
		Date date = new Date();
		String startTime = DateUtils.getAnyDayAnyYearAnyMonthString(date, -1,
				0, 1);
		String endTime = DateUtils.getSimpleDateFormat(date);
		String startTime1 = DateUtils.getAnyDayAnyYearAnyMonthString(date, -56,
				0, 1);
		String endTime1 = DateUtils.getAnyDayAnyYearAnyMonthString(date, -56,
				11, 31);
		List<Integer> plasmas = hCTAnalysisMapper.getPlasmaPeopleIds(startTime,
				endTime, startTime1, endTime1);
		return plasmas;
	}

	public String getProviderNo(List<Integer> lists) {
		StringBuilder sb = new StringBuilder();
		if (!StringUtil.isNotNull(lists)) {
			return null;
		}
		for (Integer list : lists) {
			sb.append("'" + list + "'" + ",");

		}
		return sb.substring(0, sb.length() - 1);
	}

	// getPlasmaComprehensiveMassage
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void insertGjzHCT() {
		String ProviderNo = getProviderNo(getPlasma());
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		// 浆员卡号拼接
		List<PlasmaMessage> plasmaMessages = hCTAnalysisMapper
				.getJYInformation(ProviderNo);

		Date date = new Date();
		String startTime = DateUtils.getAnyDayAnyYearAnyMonthString(date, -1,
				0, 1);
		String endTime = DateUtils.getSimpleDateFormat(date);
		int num = 0;
		for (PlasmaMessage pm : plasmaMessages) {
			num++;
			GjzHct hct = new GjzHct();
			// 主键
			hct.setId(StringUtil.getUUID());
			// 浆员地址
			hct.setAddress(pm.getAddress().substring(5));
			// 浆员年龄
			hct.setAge(pm.getAge());
			// 介绍人
			hct.setJsr(pm.getReferrerName());
			// 浆员名字
			hct.setName(pm.getName());
			// 浆员性别
			if ("1".equals(pm.getGender())) {
				hct.setSex("男");
			}
			if ("2".equals(pm.getGender())) {
				hct.setSex("女");
			}
			System.err.println(pm.getCardId());
			System.err.println("性别" + "=========================="
					+ pm.getGender());
			System.err.println("卡号" + "=========================="
					+ pm.getPid());
			hct.setProviderno(pm.getCardId());
			// 浆员护士和义工的id
			String nurse = hCTAnalysisMapper.getNurse(pm.getPid());
			String volunteer = hCTAnalysisMapper.getVolunteer(pm.getPid());
			System.err.println("护士ID：" + nurse + "=====" + "义工ID：" + volunteer);
			if (!" ".equals(nurse) || nurse != null) {
				hct.setManagenurseid(nurse);
				hct.setManagenurse(hCTAnalysisMapper.getNurseName(nurse));
			}
			if (!" ".equals(volunteer) || volunteer != null) {
				hct.setYigong(hCTAnalysisMapper.getVolunteerName(volunteer));
				hct.setYigongid(String.valueOf(volunteer));

			}
			// 浆员沟通记录
			List<String> communicationRecord = hCTAnalysisMapper
					.getCommunicationRecord(pm.getPid());
			if (StringUtil.isNotNull(communicationRecord)) {
				hct.setCommunresult(communicationRecord.get(0));
			} else {
				hct.setCommunresult(null);
			}
			// 浆员hct值setLinkstate: 0表示hct值大于6,1表示hct小于6
			if (StringUtil.isNotNull(getPlasmaHCT(pm.getCardId()))) {
				hct.setHctflag("0");
			} else {
				hct.setHctflag("1");
			}
			// 通过浆员的卡号得到浆员的层数、轮次时间
			List<ParameterResult> rs = getLayerAndCollectionDate(
					pm.getCardId(), startTime, endTime);
			// System.err.println(rs.size());
			for (ParameterResult r : rs) {
				DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				XyDmRounds xyDmRounds = hCTAnalysisMapper
						.getRoundInformation(DateUtils.getSimpleDateFormat(r
								.getCollectionDate()));
				// 2017
				if (DateUtils.getYear(date, -1).equals(xyDmRounds.getRYEAR())) {
					hct.setLastyear(xyDmRounds.getRYEAR());
				}
				// 2018
				if (DateUtils.getYear(date, 0).equals(xyDmRounds.getRYEAR())) {
					hct.setCurrentyear(xyDmRounds.getRYEAR());
				}
				String cycle = String.valueOf(r.getCycleTimes());
				String ConnectionDate = DateUtils.getSimpleDateFormat(r
						.getCollectionDate());
				if (xyDmRounds.getRYEAR().equals(DateUtils.getYear(date, -1))) {
					setGjzLcLastYearRounds(xyDmRounds.getRID(), hct, cycle
							+ ":" + ConnectionDate);
				}
				if (xyDmRounds.getRYEAR().equals(DateUtils.getYear(date, 0))) {

					setGjzLcRounds(xyDmRounds.getRID(), hct, cycle + ":"
							+ ConnectionDate);
				}
			}
			// System.err.println(getCardId());
			hct.setRemark(getHctAverage(pm.getCardId(), startTime, endTime));
			DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			gjzHctMapper.insert(hct);

			System.err.println("第" + "====================>>" + num + "次"
					+ "<<===========================" + "循环");

		}

	}

	/**
	 * 4579 hct值大于等于6的浆员
	 * 
	 * @return
	 */
	public List<String> getPlasmaHCT(String ProviderNo) {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
		Date date = new Date();
		String startTime = DateUtils.getAnyDayAnyYearAnyMonthString(date, -1,
				0, 1);
		String endTime = DateUtils.getSimpleDateFormat(date);
		List<String> plasmaHct = hCTAnalysisMapper.getPlasmaHCT(ProviderNo,
				startTime, endTime);
		return plasmaHct;

	}

	/**
	 * 得到hct平均值
	 */
	public String getHctAverage(String ProviderNo, String startTime,
			String endTime) {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
		int hctAverage = hCTAnalysisMapper.getHctAverage(ProviderNo, startTime,
				endTime);
		return String.valueOf(hctAverage);

	}

	public List<ParameterResult> getLayerAndCollectionDate(String ProviderNo,
			String startTime, String endTime) {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
		List<ParameterResult> result = hCTAnalysisMapper
				.getLayerAndCollectionDate(ProviderNo, startTime, endTime);
		return result;
	}

	/**
	 * 浆员卡号
	 * 
	 * @return
	 */
	private List<String> getCardId() {
		String ProviderNo = getProviderNo(getPlasma());
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		// 浆员卡号拼接
		List<PlasmaMessage> plasmaMessages = hCTAnalysisMapper
				.getJYInformation(ProviderNo);
		List<String> cardId = new ArrayList<String>();
		for (PlasmaMessage pm : plasmaMessages) {
			cardId.add(pm.getCardId());
		}
		return cardId;
	}

	/**
	 * 更新hct值
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateGjzHct() {
		Date date = new Date();
		String startTime = DateUtils.getAnyDayAnyYearAnyMonthString(date, -1,
				0, 1);
		String dateTime = DateUtils.getYesterdayTime(new Date(), 1);
		DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
		List<ParameterResult> parameterResult = hCTAnalysisMapper
				.getCurrentRounds(dateTime);
		// 如果当前轮次浆员的id在17-18年里面则直接更新
		for (ParameterResult pt : parameterResult) {
			if (StringUtil.containStr(pt.getProviderNo(), getCardId())) {
				GjzHct hct = new GjzHct();
				GjzHctExample example = new GjzHctExample();
				String hctAverage = getHctAverage(pt.getProviderNo(),
						startTime, dateTime);
				DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				XyDmRounds xyDmRounds = hCTAnalysisMapper
						.getRoundInformation(DateUtils.getSimpleDateFormat(pt
								.getCollectionDate()));
				example.createCriteria().andProvidernoEqualTo(
						pt.getProviderNo());
				if (StringUtil.isNotNull(getPlasmaHCT(pt.getProviderNo()))) {
					hct.setHctflag("0");
				} else {
					hct.setHctflag("1");
				}
				DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				hct.setRemark(hctAverage);
				setGjzLcRounds(xyDmRounds.getRID(), hct, pt.getCycleTimes()
						+ ":" + pt.getCollectionDate());
				gjzHctMapper.updateByExample(hct, example);
			} else {
				// 否者就是新浆员
				DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				// 浆员卡号拼接
				List<PlasmaMessage> plasmaMessages = hCTAnalysisMapper
						.getJYInformation(pt.getProviderNo());

				if (StringUtil.isNotNull(plasmaMessages)) {
					PlasmaMessage pm = plasmaMessages.get(0);

					GjzHct hct = new GjzHct();
					// 主键
					hct.setId(StringUtil.getUUID());
					// 浆员地址
					hct.setAddress(pm.getAddress().substring(5));
					// 浆员年龄
					hct.setAge(pm.getAge());
					// 介绍人
					hct.setJsr(pm.getReferrerName());
					// 浆员名字
					hct.setName(pm.getName());
					// 浆员性别
					if ("1".equals(pm.getGender())) {
						hct.setSex("男");
					}
					if ("2".equals(pm.getGender())) {
						hct.setSex("女");
					}
					hct.setProviderno(pm.getCardId());
					// 浆员护士和义工的id
					String nurse = hCTAnalysisMapper.getNurse(pm.getPid());
					String volunteer = hCTAnalysisMapper.getVolunteer(pm
							.getPid());
					if (!" ".equals(nurse) || nurse != null) {
						hct.setManagenurseid(nurse);
						hct.setManagenurse(hCTAnalysisMapper
								.getNurseName(nurse));
					}
					if (!" ".equals(volunteer) || volunteer != null) {
						hct.setYigong(hCTAnalysisMapper
								.getVolunteerName(volunteer));
						hct.setYigongid(String.valueOf(volunteer));

					}
					// 浆员沟通记录
					List<String> communicationRecord = hCTAnalysisMapper
							.getCommunicationRecord(pm.getPid());
					if (StringUtil.isNotNull(communicationRecord)) {
						hct.setCommunresult(communicationRecord.get(0));
					} else {
						hct.setCommunresult(null);
					}
					// 0表示hct值大于6,1表示hct小于6
					if (StringUtil.isNotNull(getPlasmaHCT(pm.getCardId()))) {
						hct.setHctflag("0");
					} else {
						hct.setHctflag("1");
					}
					// 轮次CycleTimes AS cycleTimes,CollectionDate AS
					// 通过浆员的卡号得到浆员的层数、轮次时间
					List<ParameterResult> rs = getLayerAndCollectionDate(
							pm.getCardId(), startTime, dateTime);
					for (ParameterResult r : rs) {
						DataSourceContextHolder
								.setDataSourceType(DataSourceConst.mx);

						XyDmRounds xyDmRounds = hCTAnalysisMapper
								.getRoundInformation(DateUtils
										.getSimpleDateFormat(r
												.getCollectionDate()));
						// 2018
						if (DateUtils.getYear(date, 0).equals(
								xyDmRounds.getRYEAR())) {
							hct.setCurrentyear(xyDmRounds.getRYEAR());
						}
						String cycle = String.valueOf(r.getCycleTimes());
						String ConnectionDate = DateUtils.getSimpleDateFormat(r
								.getCollectionDate());
						if (xyDmRounds.getRYEAR().equals(
								DateUtils.getYear(date, -1))) {
							setGjzLcLastYearRounds(xyDmRounds.getRID(), hct,
									cycle + ":" + ConnectionDate);
						}
						if (xyDmRounds.getRYEAR().equals(
								DateUtils.getYear(date, 0))) {

							setGjzLcRounds(xyDmRounds.getRID(), hct, cycle
									+ ":" + ConnectionDate);
						}
					}
					hct.setRemark(getHctAverage(pm.getCardId(), startTime,
							dateTime));
					DataSourceContextHolder
							.setDataSourceType(DataSourceConst.mx);
					gjzHctMapper.insert(hct);

				}
			}

		}

	}

	private void setGjzLcRounds(String rid, GjzHct hct, String value) {
		try {
			Class<?> clazz = Class.forName("cn.jy.map.domain.GjzHct");

			Method setRound = clazz.getMethod("setRound" + rid, String.class);

			setRound.invoke(hct, value);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setGjzLcLastYearRounds(String rid, GjzHct hct, String value) {
		try {
			Class<?> clazz = Class.forName("cn.jy.map.domain.GjzHct");

			Method setRound = clazz.getMethod("setLastround" + rid,
					String.class);

			setRound.invoke(hct, value);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<NurseResult> findCycleByNurse() throws Exception {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<NurseResult> Nurses = new ArrayList<NurseResult>();
		NurseResult nr = new NurseResult();
		nr.getMaps().put("nurseName", "所有护士");
		nr.getMaps().put("nurse", "0");
		Nurses.add(nr);
		List<Statistical> list = hCTAnalysisMapper.searchNurse();
		int total = 0;
		if (StringUtil.isNotNull(list)) {
			for (Statistical nurse : list) {
				if (!StringUtil.isNotNull(nurse.getNurseName())) {
					total += Integer.parseInt(nurse.getTotal());
				}

				if (StringUtil.isNotNull(nurse.getNurseId())) {
					NurseResult nr2 = new NurseResult();
					nr2.getMaps().put(
							"nurseName",
							nurse.getNurseName() + "【" + nurse.getTotal() + "人"
									+ "】");
					nr2.getMaps().put("nurse", nurse.getNurseId());
					Nurses.add(nr2);
				}
			}

		}
		NurseResult nr3 = new NurseResult();
		nr3.getMaps().put("nurseName", "其他" + "【" + total + "人" + "】");

		nr3.getMaps().put("nurse", "1");
		Nurses.add(nr3);
		return Nurses;
	}

	@Override
	public List<Map<String, String>> findCycleByState(String nurseId)
			throws Exception {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<Map<String, String>> State = new ArrayList<Map<String, String>>();
		List<String> hctFlag = hCTAnalysisMapper.getHctFlag();
		// 浆员hct值setLinkstate: 0表示hct值大于5,1表示hct小于5
		// 0表示hct值大于5
		Map<String, String> maps = new LinkedHashMap<String, String>();
		Map<String, String> maps2 = new LinkedHashMap<String, String>();
		List<String> hct = new ArrayList<String>();
		List<String> hct2 = new ArrayList<String>();
		hCTAnalysisMapper.getHctFlagByGroup(nurseId, hctFlag.get(0));
		if (nurseId.equals("1")) {
			hct = hCTAnalysisMapper.getHctFlagOther(hctFlag.get(1));
			maps.put("hctName", "采浆层数大于5" + "【" + hct.size() + "人" + "】");
			maps.put("hctFlag", hctFlag.get(1));
			State.add(maps);
			hct2 = hCTAnalysisMapper.getHctFlagOther(hctFlag.get(0));
			maps2.put("hctName", "采浆层数小于等于5" + "【" + hct2.size() + "人" + "】");
			maps2.put("hctFlag", hctFlag.get(0));
			State.add(maps2);

		} else if (nurseId.equals("0")) {
			hct = hCTAnalysisMapper.getAllOther(hctFlag.get(1));
			maps.put("hctName", "采浆层数大于5" + "【" + hct.size() + "人" + "】");
			maps.put("hctFlag", hctFlag.get(1));
			State.add(maps);
			hct2 = hCTAnalysisMapper.getAllOther(hctFlag.get(0));
			maps2.put("hctName", "采浆层数小于等于5" + "【" + hct2.size() + "人" + "】");
			maps2.put("hctFlag", hctFlag.get(0));
			State.add(maps2);
		} else {
			hct = hCTAnalysisMapper.getHctFlagByGroup(nurseId, hctFlag.get(1));
			maps.put("hctName", "采浆层数大于5" + "【" + hct.size() + "人" + "】");
			maps.put("hctFlag", hctFlag.get(1));
			State.add(maps);
			hct2 = hCTAnalysisMapper.getHctFlagByGroup(nurseId, hctFlag.get(0));
			maps2.put("hctName", "采浆层数小于等于5" + "【" + hct2.size() + "人" + "】");
			maps2.put("hctFlag", hctFlag.get(0));
			State.add(maps2);
		}
		return State;
	}

	@Override
	public List<Map<String, String>> findCycleByAddress(String nurseId,
			String state) throws Exception {
		List<Map<String, String>> Address = null;
		if (nurseId.equals("0") && state.equals("1")) {
			Address = searchAddress(null, "1");
			return Address;
		}
		if (nurseId.equals("0") && state.equals("0")) {
			Address = searchAddress(null, "0");
			return Address;
		}
		if (nurseId.equals("1") && state.equals("0")) {
			Address = searchAddressIsNull("0");
			return Address;
		}
		if (nurseId.equals("1") && state.equals("1")) {
			Address = searchAddressIsNull("1");
			return Address;
		}
		if (StringUtil.isNotNull(nurseId) && state.equals("1")) {
			Address = searchAddress(nurseId, "1");
			return Address;
		}
		if (StringUtil.isNotNull(nurseId) && state.equals("0")) {
			Address = searchAddress(nurseId, "0");
			return Address;
		}
		return Address;
	}

	public List<Map<String, String>> searchAddressIsNull(String state) {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<Map<String, String>> Address = new ArrayList<Map<String, String>>();
		// 所有的村落
		Map<String, String> map1 = new LinkedHashMap<String, String>();
		List<String> villages = hCTAnalysisMapper.getVillageAllIsNull(state);
		map1.put("villageName", "所有村落" + "【" + villages.size() + "个村落" + "】");
		map1.put("address", "所有村落");
		Address.add(map1);
		int num = 0;
		List<Statistical> rs = hCTAnalysisMapper.getNativeplaceIsNull(state);
		for (Statistical st : rs) {
			Map<String, String> maps = new LinkedHashMap<String, String>();
			maps.put("villageName", st.getNativeplace() + "【" + st.getTotal()
					+ "个人" + "】");
			maps.put("address", st.getNativeplace());
			Address.add(maps);
			num += Integer.valueOf(st.getTotal());
		}
		System.err.println("总数" + "============>>" + num + "<<=============");
		return Address;
	}

	public List<Map<String, String>> searchAddress(String nurseId, String state) {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<Map<String, String>> Address = new ArrayList<Map<String, String>>();
		// 所有的村落

		Map<String, String> map1 = new LinkedHashMap<String, String>();
		List<String> villages = hCTAnalysisMapper.getVillageAll(state, nurseId);
		map1.put("villageName", "所有村落" + "【" + villages.size() + "个村落" + "】");
		map1.put("address", "所有村落");

		Address.add(map1);
		int num = 0;
		List<Statistical> rs = hCTAnalysisMapper.getNativeplace(state, nurseId);
		for (Statistical st : rs) {
			Map<String, String> maps = new LinkedHashMap<String, String>();
			maps.put("villageName", st.getNativeplace() + "【" + st.getTotal()
					+ "个人" + "】");
			num += Integer.valueOf(st.getTotal());
			maps.put("address", st.getNativeplace());
			Address.add(maps);
		}
		System.err.println("总数" + "============>>" + num + "<<=============");
		return Address;

	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void update() {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<Integer> list = hCTAnalysisMapper.getOtherCycle();
		Date date = new Date();
		String startTime = DateUtils.getAnyDayAnyYearAnyMonthString(date, -1,
				0, 1);
		String dateTime = DateUtils.getYesterdayTime(new Date(), 1);
		System.err.println(startTime + "==========" + dateTime);
		for (Integer ls : list) {
			GjzHct hct = new GjzHct();
			String ProviderNo = String.valueOf(ls);
			System.err.println(ProviderNo);
			DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
			List<ParameterResult> ptList = hCTAnalysisMapper
					.getOther(ProviderNo);
			String hctAverage = getHctAverage(ProviderNo, startTime, dateTime);
			hct.setRemark(hctAverage);
			System.err.println(hctAverage);
			DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			GjzHctExample example = new GjzHctExample();
			example.createCriteria().andProvidernoEqualTo(ProviderNo);
			// List<GjzHct> gjzHct = gjzHctMapper.selectByExample(example);
			if (StringUtil.isNotNull(getPlasmaHCT(ProviderNo))) {
				hct.setHctflag("0");
			} else {
				hct.setHctflag("1");
			}
			System.err.println(hct.getHctflag());
			DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			for (ParameterResult pt : ptList) {
				XyDmRounds xyDmRounds = hCTAnalysisMapper
						.getRoundInformation(DateUtils.getSimpleDateFormat(pt
								.getCollectionDate()));
				if (DateUtils.getYear(date, -1).equals(xyDmRounds.getRYEAR())) {
					hct.setLastyear(xyDmRounds.getRYEAR());
				}
				System.err.println(hct.getLastyear());
				if (DateUtils.getYear(date, 0).equals(xyDmRounds.getRYEAR())) {
					hct.setCurrentyear(xyDmRounds.getRYEAR());
				}
				setGjzLcRounds(
						xyDmRounds.getRID(),
						hct,
						pt.getCycleTimes()
								+ ":"
								+ DateUtils.getSimpleDateFormat(pt
										.getCollectionDate()));
				gjzHctMapper.updateByExampleSelective(hct, example);

			}
		}

	}

	@Override
	public List<GjzHctResult> findCycles(String nurse, String state,
			String address) throws Exception {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<GjzHctResult> gjzHct = new ArrayList<GjzHctResult>();
		// 获取护士下面链接的浆员卡号集合
		List<String> nurselist = null;
		List<String> stateList = null;
		// 0表示全部护士
		if (StringUtil.isNotNull(nurse) && "0".equals(nurse)) {
			nurselist = hCTAnalysisMapper.getJyNoAll();
			// 1表示没有护士关联
		} else if (StringUtil.isNotNull(nurse) && "1".equals(nurse)) {
			nurselist = hCTAnalysisMapper.getJyNoIsNull();
		} else {
			nurselist = hCTAnalysisMapper.getJyNoByNurse(nurse);

		}
		if (StringUtil.isNotNull(state) && "0".equals(state)) {
			stateList = hCTAnalysisMapper.getJyNoByState("0");
		} else if (StringUtil.isNotNull(state) && "1".equals(state)) {
			stateList = hCTAnalysisMapper.getJyNoByState("1");
		}
		if (StringUtil.isNotNull(stateList)) {
			nurselist = StringUtil.retainList(nurselist, stateList);
		}
		if (StringUtil.isNotNull(nurselist)) {
			if (StringUtil.isNotNull(nurse)) {
				String providerNo = StringUtil.getStringIds(nurselist);
				gjzHct = hCTAnalysisMapper.getHctByProviderNo(providerNo);
				System.err.println("护士" + nurse + "==========>>"
						+ gjzHct.size());
				System.err.println("round1" + "======>>"
						+ gjzHct.get(0).getRound1() + "<<======>>"
						+ "lastTimePulps" + "<<======>>"
						+ gjzHct.get(0).getLastTimePulps() + "<<========");
			}
			if (StringUtil.isNotNull(state) && !StringUtil.isNotNull(address)) {
				String providerNo = StringUtil.getStringIds(nurselist);
				gjzHct = hCTAnalysisMapper.getHctByProviderNo(providerNo);
				System.err.println("护士+状态" + state + "==========>>"
						+ gjzHct.size());
				System.err.println(gjzHct.get(0).getRound1());

			}
			if (StringUtil.isNotNull(address) && !"所有村落".equals(address)) {
				System.err.println(address);
				// 获取指定浆员中，地址为指定地址的那些人
				List<String> addressList = hCTAnalysisMapper
						.getJyNoByAddress(address);
				System.err.println("addressList:" + addressList.size());
				nurselist = StringUtil.retainList(nurselist, addressList);
				System.err.println("nurselist:" + "===========>>"
						+ nurselist.size());
				String providerNo = StringUtil.getStringIds(nurselist);
				System.err.println(providerNo);
				gjzHct = hCTAnalysisMapper.getHctByProviderNo(providerNo);
				System.err.println("护士+状态+地址" + address + "==========>>"
						+ gjzHct.size());
				System.err.println(gjzHct.get(0).getRound1());
			}
			if (StringUtil.isNotNull(address) && "所有村落".equals(address)) {
				// 获取指定浆员中，地址为指定地址的那些人
				String providerNo = StringUtil.getStringIds(nurselist);
				gjzHct = hCTAnalysisMapper.getHctByProviderNo(providerNo);
				System.err.println("address为所有村落的时候走了这个方法");
				System.err.println(gjzHct.get(0).getRound1());
			}

		}

		return gjzHct;
	}

	public List<GjzHctResult> searchCyclesByAddress(String state,
			String address, String nurse) {
		System.err.println();
		List<GjzHctResult> gjzHct = hCTAnalysisMapper.searchCyclesByAddress(
				state, address, nurse);
		System.err.println("==============>>" + gjzHct.size()
				+ "<<=================");
		return gjzHct;
	}

	public List<GjzHctResult> searchCyclesNotNurse(String state) {
		List<GjzHctResult> gjzHct = hCTAnalysisMapper.getCyclesNotNurse(state);
		System.err.println("searchCyclesNotNurse:" + gjzHct.size());
		return gjzHct;
	}

	public List<GjzHctResult> searchCyclesByHct(String state, String nurse) {
		List<GjzHctResult> gjzHct = hCTAnalysisMapper.getCyclesByNurse(state,
				nurse);
		System.err.println("searchCyclesByHct:" + gjzHct.size());
		return gjzHct;
	}

	@Override
	public void test() {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		String ids = StringUtil.getStringIds(updateHctMapper.getPlasmaMember());
		List<String> list2 = updateHctMapper.getProviderNo1();
		int num = 0;
		// for (String providerNo : list2) {
		// List<String> name = updateHctMapper.searchName(providerNo);
		// GjzHctExample example = new GjzHctExample();
		// example.createCriteria().andProvidernoEqualTo(providerNo);
		// List<GjzHct> hct1 = gjzHctMapper.selectByExample(example);
		// hct1.get(0).setTimes(name.get(0));
		// gjzHctMapper.updateByExampleSelective(hct1.get(0), example);
		// num++;
		// System.err
		// .println("更新了第============>>" + num + "<<==============次");
		// // break;
		//
		// }

		DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
		Date date = new Date();
		String startTime = DateUtils.getAnyDayAnyYearAnyMonthString(date, -1,
				0, 1);
		String endTime = DateUtils.getSimpleDateFormat(date);
		List<String> list1 = updateHctMapper.getPlasmaNumber(ids, startTime,
				endTime);
		List<String> list3 = StringUtil.removeList(list1, list2);
		System.err.println("searchCyclesByHct:" + list3.size());

		String numbers = StringUtil.getStringIds(updateHctMapper
				.getPlasmaNumber(StringUtil.getStringIds(list3), startTime,
						endTime));
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<GjzHct> list = updateHctMapper
				.getPlasmaComprehensiveInformation(numbers);
		for (GjzHct gjzHct : list) {
			GjzHct hct = new GjzHct();
			hct.setId(StringUtil.getUUID());
			hct.setSex(gjzHct.getSex());
			hct.setAge(gjzHct.getAge());
			hct.setPulpage(gjzHct.getPulpage());
			hct.setProviderno(gjzHct.getProviderno());
			hct.setName(gjzHct.getName());
			hct.setAddress(gjzHct.getAddress());
			hct.setManagenurse(gjzHct.getManagenurse());
			hct.setManagenurseid(gjzHct.getManagenurseid());
			hct.setJsr(gjzHct.getJsr());
			hct.setYigong(gjzHct.getYigong());
			hct.setYigongid(gjzHct.getYigongid());
			hct.setPulpage(gjzHct.getPulpage());
			hct.setCommunresult(gjzHct.getCommunresult());
			hct.setLastyearpulps(gjzHct.getLastyearpulps());
			hct.setCurrenttimepulps(gjzHct.getCurrenttimepulps());
			hct.setLasttimepulps(gjzHct.getLasttimepulps());
			if (StringUtil.isNotNull(getPlasmaHCT(gjzHct.getProviderno()))) {
				hct.setHctflag("0");
			} else {
				hct.setHctflag("1");
			}
			DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
			List<ParameterResult> rs = updateHctMapper
					.getLayerAndCollectionDate(gjzHct.getProviderno(),
							startTime, endTime);
			System.err.println("=================>>" + rs.size()
					+ "<<==================");
			for (ParameterResult r : rs) {
				DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				XyDmRounds xyDmRounds = hCTAnalysisMapper
						.getRoundInformation(DateUtils.getSimpleDateFormat(r
								.getCollectionDate()));
				if (DateUtils.getYear(date, -1).equals(xyDmRounds.getRYEAR())) {
					hct.setLastyear(xyDmRounds.getRYEAR());
				}
				// 2018
				if (DateUtils.getYear(date, 0).equals(xyDmRounds.getRYEAR())) {
					hct.setCurrentyear(xyDmRounds.getRYEAR());
				}
				String cycle = String.valueOf(r.getCycleTimes());
				String ConnectionDate = DateUtils.getSimpleDateFormat(r
						.getCollectionDate());
				if (xyDmRounds.getRYEAR().equals(DateUtils.getYear(date, -1))) {
					setGjzLcLastYearRounds(xyDmRounds.getRID(), hct, cycle
							+ ":" + ConnectionDate);
				}
				if (xyDmRounds.getRYEAR().equals(DateUtils.getYear(date, 0))) {

					setGjzLcRounds(xyDmRounds.getRID(), hct, cycle + ":"
							+ ConnectionDate);
				}
			}
			hct.setRemark(getHctAverage(gjzHct.getProviderno(), startTime,
					endTime));
			DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			gjzHctMapper.insert(hct);
			num++;
			System.err
					.println("循环了第============>>" + num + "<<==============次");
		}

	}

	public void updatePlasmaHct() {

	}

	@Override
	public Map<String, String> getPlasmaMemberInformation(String providerNo,
			String connectionTime) throws Exception {
		Map<String, String> result = new LinkedHashMap<String, String>();
		DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
		List<NurseResult> nurseResult = updateHctMapper
				.getPlasmaMemberInformation(providerNo, connectionTime);
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		String nurseId = "mxz" + nurseResult.get(0).getCollectionWorker();
		String connectNurse = updateHctMapper.getConnectNurse(nurseId);
		result.put("machineNo", nurseResult.get(0).getMachineNo());
		result.put("connectNurse", connectNurse);
		result.put("runTime", nurseResult.get(0).getRunTime() + "分钟");

		return result;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void updatePlasmaMemberHct() {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<String> list1 = updateHctMapper.getProviderNo1();
		DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
		Date date = new Date();
		String dateTime = DateUtils.getYesterdayTime(new Date(), 1);
		String startTime = DateUtils.getAnyDayAnyYearAnyMonthString(date, -1,
				0, 1);
		String emdTime17 = DateUtils.getAnyDayAnyYearAnyMonthString(date, -1,
				11, 31);
		List<String> list2 = updateHctMapper
				.getPlasmaByConnectionTime(dateTime);
		List<String> list3 = StringUtil.retainList(list1, list2);
		List<String> list4 = StringUtil.removeList(list3, list2);
		// 如果包含直接更新
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		if (StringUtil.isNotNull(list3)) {
			for (String providerNo : list3) {
				GjzHctExample example = new GjzHctExample();
				example.createCriteria().andProvidernoEqualTo(providerNo);
				List<GjzHct> hct = gjzHctMapper.selectByExample(example);

				DataSourceContextHolder
						.setDataSourceType(DataSourceConst.jingying);
				// 更新hct平均值
				String hctAvg = getHctAverage(providerNo, startTime, dateTime);
				hct.get(0).setRemark(hctAvg);
				// 上一年的轮次的更新
				List<NurseResult> lastYear = updateHctMapper
						.getYearCycleAndConnectionTime(providerNo, startTime,
								emdTime17);
				if (StringUtil.isNotNull(lastYear)) {
					for (NurseResult list : lastYear) {
						DataSourceContextHolder
								.setDataSourceType(DataSourceConst.mx);
						XyDmRounds xyDmRounds = hCTAnalysisMapper
								.getRoundInformation(DateUtils
										.getSimpleDateFormat(list
												.getCollectionDate()));
						if (xyDmRounds.getRYEAR().equals(
								DateUtils.getYear(new Date(), -1))) {
							hct.get(0).setLastyear(xyDmRounds.getRYEAR());
						}
						setGjzLcLastYearRounds(
								xyDmRounds.getRID(),
								hct.get(0),
								list.getCycleTimes() + ":"
										+ list.getCollectionDate());
					}

				}
				DataSourceContextHolder
						.setDataSourceType(DataSourceConst.jingying);
				List<NurseResult> ct = updateHctMapper
						.getCycleAndConnectionTime(providerNo, dateTime);
				String cycle = ct.get(0).getCycleTimes();
				String collectionDate = DateUtils.getSimpleDateFormat(ct.get(0)
						.getCollectionDate());
				DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				XyDmRounds xyDmRounds = hCTAnalysisMapper
						.getRoundInformation(collectionDate);
				if (xyDmRounds.getRYEAR().equals(
						DateUtils.getYear(new Date(), -1))) {
					hct.get(0).setCurrentyear(xyDmRounds.getRYEAR());
				}
				// 更新轮次
				setGjzLcRounds(xyDmRounds.getRID(), hct.get(0), cycle + ":"
						+ collectionDate);
				// 更新hctflag
				if (Integer.parseInt(cycle) >= 6) {
					hct.get(0).setHctflag("0");
				} else {
					hct.get(0).setHctflag("1");
				}
				List<GjzHct> gjzHct = updateHctMapper
						.getPlasmaComprehensiveInformation(providerNo);
				hct.get(0).setManagenurse(gjzHct.get(0).getManagenurse());
				hct.get(0).setManagenurseid(gjzHct.get(0).getManagenurseid());
				hct.get(0).setPulpage(gjzHct.get(0).getPulpage());
				hct.get(0).setLastyearpulps(gjzHct.get(0).getLastyearpulps());
				hct.get(0).setCurrenttimepulps(
						gjzHct.get(0).getCurrenttimepulps());
				hct.get(0).setLasttimepulps(gjzHct.get(0).getLasttimepulps());
				gjzHctMapper.updateByExampleSelective(hct.get(0), example);

			}
		}
		// 如果不包含则是新浆员
		if (StringUtil.isNotNull(list4)) {
			for (String providerNo : list4) {
				GjzHct hct = new GjzHct();
				DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				List<GjzHct> lc = updateHctMapper
						.getPlasmaComprehensiveInformation(providerNo);
				hct.setId(StringUtil.getUUID());
				hct.setProviderno(providerNo);
				hct.setName(lc.get(0).getName());
				hct.setSex(lc.get(0).getSex());
				hct.setAge(lc.get(0).getAge());
				hct.setAddress(lc.get(0).getAddress());
				hct.setManagenurse(lc.get(0).getManagenurse());
				hct.setManagenurseid(lc.get(0).getManagenurseid());
				hct.setJsr(lc.get(0).getJsr());
				hct.setPulpage(lc.get(0).getPulpage());
				hct.setCommunresult(lc.get(0).getCommunresult());
				hct.setLastyearpulps(lc.get(0).getLastyearpulps());
				hct.setCurrenttimepulps(lc.get(0).getCurrenttimepulps());
				hct.setLasttimepulps(lc.get(0).getLasttimepulps());
				hct.setYigong(lc.get(0).getYigong());
				hct.setYigongid(lc.get(0).getYigongid());
				hct.setCommunresult(lc.get(0).getCommunresult());
				DataSourceContextHolder
						.setDataSourceType(DataSourceConst.jingying);
				String hctAvg = getHctAverage(providerNo, startTime, dateTime);
				List<NurseResult> ct = updateHctMapper
						.getCycleAndConnectionTime(providerNo, dateTime);
				String cycle = ct.get(0).getCycleTimes();
				String collectionDate = DateUtils.getSimpleDateFormat(ct.get(0)
						.getCollectionDate());
				// 自己算
				hct.setRemark(hctAvg);
				// 自己算
				// 更新hctflag
				if (Integer.parseInt(cycle) >= 6) {
					hct.setHctflag("0");
				} else {
					hct.setHctflag("1");
				}
				// 上一年的轮次的更新
				List<NurseResult> lastYear = updateHctMapper
						.getYearCycleAndConnectionTime(providerNo, startTime,
								emdTime17);
				if (StringUtil.isNotNull(lastYear)) {
					for (NurseResult list : lastYear) {
						DataSourceContextHolder
								.setDataSourceType(DataSourceConst.mx);
						XyDmRounds xyDmRounds = hCTAnalysisMapper
								.getRoundInformation(DateUtils
										.getSimpleDateFormat(list
												.getCollectionDate()));
						if (xyDmRounds.getRYEAR().equals(
								DateUtils.getYear(new Date(), -1))) {
							hct.setLastyear(xyDmRounds.getRYEAR());
						}
						setGjzLcLastYearRounds(
								xyDmRounds.getRID(),
								hct,
								list.getCycleTimes() + ":"
										+ list.getCollectionDate());
					}

				}
				DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				XyDmRounds xyDmRounds = hCTAnalysisMapper
						.getRoundInformation(collectionDate);
				if (xyDmRounds.getRYEAR().equals(
						DateUtils.getYear(new Date(), -1))) {
					hct.setCurrentyear(xyDmRounds.getRYEAR());
				}

				setGjzLcRounds(xyDmRounds.getRID(), hct, cycle + ":"
						+ collectionDate);

				gjzHctMapper.insert(hct);
			}
		}
	}
}
